import Base64 from "./base64.js"
import store from "../store/index.js"

export default class Parse {

  // private property  

  static getInstance() {
    if (!this.instance) {
      this.instance = new Parse();
    }
    return this.instance
  }

  info = {
      stationId: undefined,
      labelId: undefined,
      employeeId: undefined,
      userScan: {
        gunName: undefined,
        receivableAmt: undefined,
        orderRandomId: undefined,
        orderOriginType: undefined,
      }
    }
  // public method for decoding  
  _processor(res) {
    const base64 = Base64.getInstance();
    let data = res.replace("https://skip.huijy.net/", "").replace("https://jump.huijy.net/", "");
    let stationId
    let employeeId
    let labelId
    // let info = {
    //   stationId: undefined,
    //   labelId: undefined,
    //   employeeId: undefined,
    //   userScan: {
    //     gunName: undefined,
    //     receivableAmt: undefined,
    //     orderRandomId: undefined,
    //     orderOriginType: undefined,
    //   }
    // }
    console.log(data)
    const idArr = data.match(/^\d{1,}/g)
    if (idArr === null) {
      throw new Error("二维码是个啥玩意？！")
    }
    // 处理后的data
    data = data.replace(/^\d{1,}\//g, '')
    // 解压后的data
    data = base64.decode(data)

    // 员工id
    if (data.includes('e')) {
      const employeeIdStrArr = data.match(/e\d{1,}/g)
      if (employeeIdStrArr === null) {
        return
      }
      const employeeIdArr = employeeIdStrArr[0].match(/\d{1,}/g)
      if (employeeIdArr === null) {
        return
      }
      employeeId = employeeIdArr[0]
      this.info.employeeId = employeeId
      store.commit('updateEmployeeId', employeeId);
    }

    // 标签id
    if (data.includes('l')) {
      const labelIdStrArr = data.match(/l\d{1,}/g)
      if (labelIdStrArr === null) {
        return
      }
      const labelIdArr = labelIdStrArr[0].match(/\d{1,}/g)
      if (labelIdArr === null) {
        return
      }
      labelId = labelIdArr[0]
      this.info.labelId = labelId
      store.commit("updateLabelId", labelId)
    }
    
    // receivableAmt应付款 m
    if (data.includes('m') && data.includes('t')) {
      const receivableAmtStrArr = data.match(/m\d{1,}/g)
      if (receivableAmtStrArr === null) {
        return
      }
      const receivableAmtArr = receivableAmtStrArr[0].match(/\d{1,}/g)
      if (receivableAmtArr === null) {
        return
      }
      this.info.userScan.receivableAmt = receivableAmtArr[0]
    }

    // gunName 枪号 g 
    if (data.includes('g') && data.includes('t')) {
      const gunNameStrArr = data.match(/g\d{1,}/g)
      if (gunNameStrArr === null) {
        return
      }
      
      const gunNameArr = gunNameStrArr[0].match(/\d{1,}/g)
      if (gunNameArr === null) {
        return
      }
      this.info.userScan.gunName = gunNameArr[0]
    }

    // randomId 随机号 r
    if (data.includes('r') && data.includes('t')) {
      const orderRandomIdStrArr = data.match(/r\d{1,}/g)
      if (orderRandomIdStrArr === null) {
        return
      }
      const orderRandomIdArr = orderRandomIdStrArr[0].match(/\d{1,}/g)
      if (orderRandomIdArr === null) {
        return
      }
      this.info.userScan.orderRandomId = orderRandomIdArr[0]
    }
    
    // orderOriginType
    if (data.includes('t')) {
      const orderOriginTypeStrArr = data.match(/t\d{1,}/g)
      if (orderOriginTypeStrArr === null) {
        return
      }
      const orderOriginTypeArr = orderOriginTypeStrArr[0].match(/\d{1,}/g)
      if (orderOriginTypeArr === null) {
        return
      }
      this.info.userScan.orderOriginType = orderOriginTypeArr[0]
      if (
        !this.info.userScan.gunName ||
        !this.info.userScan.receivableAmt ||
        !this.info.userScan.orderRandomId ||
        !this.info.userScan.orderOriginType
      ) { 
        // info.userScan = {
        //   orderOriginType:'-1'
        // }
        throw new Error("信息不全")
      }
      
      store.commit("updateUserScan", this.info.userScan)
    }

    // 放到后面
    stationId = idArr[0]
    this.info.stationId = stationId
    store.commit("updateStationId", stationId)
    return;
  }

  async parseCode(res) {
    try {
      this._processor(res)
      console.log('扫码解析结果',this.info)
      return Promise.resolve(this.info)      
    } catch (e) {
      console.log(e)
      return Promise.reject("亲，请扫油站提供的码呦~")
    }
  }

  async _scanCode() {
    try {
      const scanCodePro = await uni.scanCode({
        onlyFromCamera: true
      })
      if (scanCodePro[0] != null && scanCodePro[0].errMsg == "scanCode:fail") {
        throw new Error(1)
      }
      if (scanCodePro[0] != null) {
        throw new Error(2)
      }
      return this.parseCode(scanCodePro[1].result)
      // return Promise.resolve(scanCodePro[1].result)
    } catch (e) {
      if (e.message == 1) {
        return Promise.reject("亲，请扫油站提供的码呦~")
      } else {
        return Promise.reject("亲，请扫二维码呀~")
      }
    }
  }

  async scanCode(showModalFlag = true, moduleConfig = {}) { //msg 消息 
    let showModalPro = [
      null,
      {
        cancel: false,
        confirm: true
      }
    ]
    if (showModalFlag) {
      showModalPro = await uni.showModal({
        title: moduleConfig.title || '(～￣▽￣)～',
        showCancel: moduleConfig.showCancel,
        content: moduleConfig.content || '亲，请扫码使用~',
        cancelText: moduleConfig.cancelText || '返回',
        confirmText: moduleConfig.confirmText || '立刻扫码',
      })
    }

    if (showModalPro[1].cancel) {
      uni.navigateBack({
        delta: 1
      })
      return Promise.reject("返回")
    } else if (showModalPro[1].confirm) {
      return this._scanCode().catch((errMsg) => {
        return this.scanCode(true, {
          content: errMsg
        })
      });
    }
  }

  async onlyScanCode() { 
    return this._scanCode()
  }


}
